package com.wallace.downloadserver.service.impl

import com.wallace.downloadserver.bean.User
import com.wallace.downloadserver.mapper.UserMapper
import com.wallace.downloadserver.pojo.LoginData
import com.wallace.downloadserver.service.UserService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

/**
 * @Author: Wallace
 * @Description:
 * @Date: 2022/1/14 22:12
 * @Modified By:
 */
@Service
class UserServiceImpl : UserService {

    @Autowired
    private val userMapper: UserMapper? = null

    private val phoneNumberRegex = "1\\d{10}"
    private val emailRegex = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*"
    private val idCardRegex = "\\d{15}(\\d\\d[0-9xX])?"


    override fun deleteByPrimaryKey(uid: String?): Int {
        TODO("Not yet implemented")
    }

    override fun insert(record: User?): Int {
        TODO("Not yet implemented")
    }

    override fun insertSelective(record: User?): Int {
        TODO("Not yet implemented")
    }

    override fun selectByPrimaryKey(uid: String?): User? {
        return userMapper!!.selectByPrimaryKey(uid)
    }

    override fun selectByLoginData(loginData: LoginData): User? {
        val u = loginData.username
        val p = loginData.password
        var user: User?
        if (u.matches(Regex(phoneNumberRegex))) {
            user = userMapper!!.selectByPhoneAndPassword(u, p)
            user?.let {
                return it
            }
        }
        if (u.matches(Regex(emailRegex))) {
            user = userMapper!!.selectByEmailAndPassword(u, p)
            user?.let {
                return it
            }
        }
        if (u.matches(Regex(idCardRegex))) {
            user = userMapper!!.selectByIdCardAndPassword(u, p)
            user?.let {
                return it
            }
        }
        return userMapper!!.selectByUsernameAndPassword(u, p)
    }

    override fun updateByPrimaryKeySelective(record: User?): Int {
        TODO("Not yet implemented")
    }

    override fun updateByPrimaryKey(record: User?): Int {
        TODO("Not yet implemented")
    }


}